namespace Glue.Internals
{
    internal class RelationDirectionSolver
    {
        public RelationDirection GetDirection(IObjectMember leftProperty, IObjectMember rightProperty)
        {
            return GetDirection(leftProperty, rightProperty, false, false);
        }
        public RelationDirection GetDirection(IObjectMember leftProperty, IObjectMember rightProperty, bool isLeftPropertyIgnored, bool isRightPropertyIgnored)
        {
            var direction = new Direction();
            if (!leftProperty.CanGetValues ||
                !rightProperty.CanGetValues)
                direction.Ignore();
            if (isLeftPropertyIgnored || (!leftProperty.CanSetValues)) // && !leftProperty.IsNested()))
                direction.IgnoreTowardsLeft();
            if (isRightPropertyIgnored || (!rightProperty.CanSetValues)) // && !rightProperty.IsNested()))
                direction.IgnoreTowardsRight();
            return direction.direction;
        }

        private class Direction
        {
            public RelationDirection direction = RelationDirection.TwoWays;

            public void Ignore()
            {
                direction = RelationDirection.None;
            }

            internal void IgnoreTowardsLeft()
            {
                if (direction == RelationDirection.TowardsLeft)
                    direction = RelationDirection.None;
                else
                    direction = RelationDirection.TowardsRight;
            }

            internal void IgnoreTowardsRight()
            {
                if (direction == RelationDirection.TowardsRight)
                    direction = RelationDirection.None;
                else
                    direction = RelationDirection.TowardsLeft;
            }
        }
    }
}